package com.hulk.dryad.manage.framework.logstash.config;

import com.hulk.dryad.manage.framework.ftp.config.FtpProperties;
import com.hulk.dryad.manage.framework.logstash.appender.ILoggingAppender;
import com.hulk.dryad.manage.framework.logstash.appender.LoggingLogStashAppender;
import com.hulk.dryad.manage.framework.logstash.listener.LogbackLoggerContextListener;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.List;

/**
 * Logstash 日志配置
 *
 * @author hulk
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(LoggingLogstashProperties.class)
@ConditionalOnProperty(name = "enabled", prefix = LoggingLogstashProperties.PREFIX, havingValue = "true")
public class LoggingLogStashConfiguration {


	@Bean
	public LogbackLoggerContextListener logbackLoggerContextListener(ObjectProvider<List<ILoggingAppender>> provider) {
		List<ILoggingAppender> loggingAppenderList = provider.getIfAvailable(ArrayList::new);
		return new LogbackLoggerContextListener(loggingAppenderList);
	}


	@Bean
	public LoggingLogStashAppender loggingLogStashAppender(Environment environment,
														   LoggingLogstashProperties properties) {
		return new LoggingLogStashAppender(environment, properties);
	}



}
